<<<<<<< HEAD
package com.wwx.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.wwx.dao.CartoonDao;
import com.wwx.dto.CartoonVO;
import com.wwx.entity.CartoonDO;
import com.wwx.entity.UserDO;
import com.wwx.util.JDBC;

public class CartoonDaoImpl implements CartoonDao{

	@Override
	public List<CartoonDO> getSelectByKey(String key, String value, Connection conn)
			throws Exception {
		// TODO Auto-generated method stub
		List<CartoonDO> list=new ArrayList<CartoonDO>();
		String sql="select a.* "
				+ " from t_cartoon as a,t_info as b,t_writer as c"
				+ " where a.cartoonid=b.cartoonid and b.wid=c.wid ";
		System.out.println("key:"+key+" value:"+value);
		if(!"".equals(value)){
//			System.out.println("sql的A拼接！");
			if(!"".equals(key)){
//				System.out.println("sql的B拼接！");
				sql+=" and "+key+" like ? ";
			}else{
//				System.out.println("sql的C拼接！");
				sql+=" and (cartoonname like ? or wname like ? )";
			}
		}
		System.out.println(sql+":显示");
		PreparedStatement p=conn.prepareStatement(sql);
		if(!"".equals(value)){
//			System.out.println("赋值A!");
			if(!"".equals(key)){
//				System.out.println("赋值B!");
				p.setString(1, "%"+value+"%");
			}else{
//				System.out.println("赋值C!");
				p.setString(1, "%"+value+"%");
				p.setString(2, "%"+value+"%");
			}
		}
		ResultSet r=p.executeQuery();
		while(r.next()){
			CartoonDO c=new CartoonDO(r.getInt("cartoonid"),r.getString("cartoonname"),
					r.getString("carurl"),r.getString("cartype"));
			list.add(c);
		}
		return list;
	}

	@Override
	public boolean innertCartoon(CartoonDO cart,Connection conn) throws Exception {
		// TODO Auto-generated method stub
		String sql="insert into t_cartoon (cartoonname,carurl,cartype) values(?,?,?)";
		PreparedStatement p=conn.prepareStatement(sql);
		p.setString(1, cart.getCartoonname());
		p.setString(2, cart.getCarurl());
		p.setString(3, cart.getCartype());
		int number=p.executeUpdate();
		if(number>0){
			return true;
		}
		return false;
	}

	@Override
	public CartoonDO byIdCartoon(int id, Connection conn) {
		CartoonDO c=null;
		String sql ="SELECT * FROM t_cartoon WHERE cartoonid = ?";
		try {
			PreparedStatement p=conn.prepareStatement(sql);
			p.setInt(1, id);
			ResultSet r=p.executeQuery();
			if(r.next()){
				c=new CartoonDO(r.getInt("cartoonid"),r.getString("cartoonname"),
						r.getString("carurl"),r.getString("cartype"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c;
	}

	@Override
	public List<CartoonDO> selectAllCartoon(Connection conn) throws Exception {
		// TODO Auto-generated method stub
		String sql="select * from t_cartoon ";
		List<CartoonDO> list=new ArrayList<CartoonDO>();
		PreparedStatement p=conn.prepareCall(sql);
		ResultSet r=p.executeQuery();
		while(r.next()){
			CartoonDO c=new CartoonDO(r.getInt("cartoonid"),
					r.getString("cartoonname"),r.getString("carurl"),
					r.getString("cartype"));
			list.add(c);
		}
		return list;
	}

	@Override
	public List<CartoonDO> selectAllCartoonPage(CartoonDO cartoonDO,int start,int length){
		// TODO Auto-generated method stub
		Configuration cfg=JDBC.getConfiguration();
		SessionFactory sessionFactory=cfg.buildSessionFactory();
		Session session=sessionFactory.openSession();
		String hql="";
		if(cartoonDO!=null) {
			if(cartoonDO.getCartoonname()!=null&&!"".equals(cartoonDO.getCartoonname())){
				System.out.println("----------------:"+cartoonDO.getCartoonname());
				hql="from CartoonDO as a where a.cartoonname like '%"+cartoonDO.getCartoonname()+"%'";
			}else {
				hql="from CartoonDO";
			}
		}else {
			System.out.println("cartoonDO为空");
			hql="from CartoonDO";
		}
		Query query=session.createQuery(hql);
		query.setFirstResult(start);
		query.setMaxResults(length);
		List<CartoonDO> cartoonDOList=query.list();
		session.close();
		return cartoonDOList;
	}

	@Override
	public Integer selectAllCartoonPageNumber(CartoonDO cartoonDO) {
		// TODO Auto-generated method stub
		Configuration cfg=JDBC.getConfiguration();
		SessionFactory sessionFactory=cfg.buildSessionFactory();
		Session session=sessionFactory.openSession();
		String hql="";
		if(cartoonDO!=null) {
			if(cartoonDO.getCartoonname()!=null&&!"".equals(cartoonDO.getCartoonname())){
				System.out.println("----------------:"+cartoonDO.getCartoonname());
				hql="select count(*) as sum  from CartoonDO as a where a.cartoonname like '%"+cartoonDO.getCartoonname()+"%'";
			}else {
				hql="select count(*) as sum  from CartoonDO";
			}
		}else {
			System.out.println("cartoonDO为空");
			hql="select count(*) as sum  from CartoonDO";
		}
		Query query=session.createQuery(hql);
		String sum= query.uniqueResult().toString();
		session.close();
		if(sum!=null) {
			System.out.println("--------------------------"+"sum为null:"+sum);
			return Integer.parseInt(sum);
		}
		return 0;
	}
}
=======
package com.wwx.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.wwx.dao.CartoonDao;
import com.wwx.dto.CartoonVO;
import com.wwx.entity.CartoonDO;
import com.wwx.entity.UserDO;
import com.wwx.util.JDBC;

public class CartoonDaoImpl implements CartoonDao{

	@Override
	public List<CartoonDO> getSelectByKey(String key, String value, Connection conn)
			throws Exception {
		// TODO Auto-generated method stub
		List<CartoonDO> list=new ArrayList<CartoonDO>();
		String sql="select a.* "
				+ " from t_cartoon as a,t_info as b,t_writer as c"
				+ " where a.cartoonid=b.cartoonid and b.wid=c.wid ";
		System.out.println("key:"+key+" value:"+value);
		if(!"".equals(value)){
//			System.out.println("sql的A拼接！");
			if(!"".equals(key)){
//				System.out.println("sql的B拼接！");
				sql+=" and "+key+" like ? ";
			}else{
//				System.out.println("sql的C拼接！");
				sql+=" and (cartoonname like ? or wname like ? )";
			}
		}
		System.out.println(sql+":显示");
		PreparedStatement p=conn.prepareStatement(sql);
		if(!"".equals(value)){
//			System.out.println("赋值A!");
			if(!"".equals(key)){
//				System.out.println("赋值B!");
				p.setString(1, "%"+value+"%");
			}else{
//				System.out.println("赋值C!");
				p.setString(1, "%"+value+"%");
				p.setString(2, "%"+value+"%");
			}
		}
		ResultSet r=p.executeQuery();
		while(r.next()){
			CartoonDO c=new CartoonDO(r.getInt("cartoonid"),r.getString("cartoonname"),
					r.getString("carurl"),r.getString("cartype"));
			list.add(c);
		}
		return list;
	}

	@Override
	public boolean innertCartoon(CartoonDO cart,Connection conn) throws Exception {
		// TODO Auto-generated method stub
		String sql="insert into t_cartoon (cartoonname,carurl,cartype) values(?,?,?)";
		PreparedStatement p=conn.prepareStatement(sql);
		p.setString(1, cart.getCartoonname());
		p.setString(2, cart.getCarurl());
		p.setString(3, cart.getCartype());
		int number=p.executeUpdate();
		if(number>0){
			return true;
		}
		return false;
	}

	@Override
	public CartoonDO byIdCartoon(int id, Connection conn) {
		CartoonDO c=null;
		String sql ="SELECT * FROM t_cartoon WHERE cartoonid = ?";
		try {
			PreparedStatement p=conn.prepareStatement(sql);
			p.setInt(1, id);
			ResultSet r=p.executeQuery();
			if(r.next()){
				c=new CartoonDO(r.getInt("cartoonid"),r.getString("cartoonname"),
						r.getString("carurl"),r.getString("cartype"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c;
	}

	@Override
	public List<CartoonDO> selectAllCartoon(Connection conn) throws Exception {
		// TODO Auto-generated method stub
		String sql="select * from t_cartoon ";
		List<CartoonDO> list=new ArrayList<CartoonDO>();
		PreparedStatement p=conn.prepareCall(sql);
		ResultSet r=p.executeQuery();
		while(r.next()){
			CartoonDO c=new CartoonDO(r.getInt("cartoonid"),
					r.getString("cartoonname"),r.getString("carurl"),
					r.getString("cartype"));
			list.add(c);
		}
		return list;
	}

	@Override
	public List<CartoonDO> selectAllCartoonPage(CartoonDO cartoonDO,int start,int length){
		// TODO Auto-generated method stub
		Configuration cfg=JDBC.getConfiguration();
		SessionFactory sessionFactory=cfg.buildSessionFactory();
		Session session=sessionFactory.openSession();
		String hql="";
		if(cartoonDO!=null) {
			if(cartoonDO.getCartoonname()!=null&&!"".equals(cartoonDO.getCartoonname())){
				System.out.println("----------------:"+cartoonDO.getCartoonname());
				hql="from CartoonDO as a where a.cartoonname like '%"+cartoonDO.getCartoonname()+"%'";
			}else {
				hql="from CartoonDO";
			}
		}else {
			System.out.println("cartoonDO为空");
			hql="from CartoonDO";
		}
		Query query=session.createQuery(hql);
		query.setFirstResult(start);
		query.setMaxResults(length);
		List<CartoonDO> cartoonDOList=query.list();
		session.close();
		return cartoonDOList;
	}

	@Override
	public Integer selectAllCartoonPageNumber(CartoonDO cartoonDO) {
		// TODO Auto-generated method stub
		Configuration cfg=JDBC.getConfiguration();
		SessionFactory sessionFactory=cfg.buildSessionFactory();
		Session session=sessionFactory.openSession();
		String hql="";
		if(cartoonDO!=null) {
			if(cartoonDO.getCartoonname()!=null&&!"".equals(cartoonDO.getCartoonname())){
				System.out.println("----------------:"+cartoonDO.getCartoonname());
				hql="select count(*) as sum  from CartoonDO as a where a.cartoonname like '%"+cartoonDO.getCartoonname()+"%'";
			}else {
				hql="select count(*) as sum  from CartoonDO";
			}
		}else {
			System.out.println("cartoonDO为空");
			hql="select count(*) as sum  from CartoonDO";
		}
		Query query=session.createQuery(hql);
		String sum= query.uniqueResult().toString();
		session.close();
		if(sum!=null) {
			System.out.println("--------------------------"+"sum为null:"+sum);
			return Integer.parseInt(sum);
		}
		return 0;
	}
}
>>>>>>> dev
